home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 May (DVD) / Macworld Resource DVD May 2003.toast / Data / Software / Bonus / Programming / revolutionosx.sit / Revolution 1.1.1 / External SDK / XtGlue.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-21  |  5.9 KB  |  252 lines  |  [????/????]

  1. /********************************************/
  2. /*    Copyright 1997 MetaCard Corporation   */
  3. /*    All Rights Reserved                   */
  4. /********************************************/
  5. /*
  6.  * Support routines for MetaCard external commands and functions
  7.  */
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <values.h>
  11. #include <fcntl.h>
  12. #ifdef SELECT
  13. #ifndef LINUX
  14. /* some systems don't have select.h and/or stream.h.  If yours doesn't
  15.    delete the offending #include */
  16. #include <sys/select.h>
  17. #include <sys/stream.h>
  18. #endif
  19. #else
  20. #include <poll.h>
  21. #endif
  22. #include <X11/Xlib.h>
  23. #include <X11/Xos.h>
  24. #include <X11/Xatom.h>
  25. #include <X11/Intrinsic.h>
  26. #include "XCmdGlue.h"
  27. #include "XCmdName.h"
  28.  
  29. #define XATOMSIZE 16
  30. #define XINTSTRSIZE 16
  31. #define XMAXPACK 65535
  32.  
  33. extern int wait();
  34.  
  35. Display *MCdpy;
  36. Window MCwin = 0;
  37. Widget MCwidget;
  38. int MCpid;
  39.  
  40. static char *XCAtomNames[MC_NUM_ATOMS] = {
  41.   MC_ID_STRING,
  42.   MC_ABORT_STRING,
  43.   MC_REGISTER_STRING,
  44.   MC_CARD_MESSAGE_STRING,
  45.   MC_MC_MESSAGE_STRING,
  46.   MC_EVAL_EXPRESSION_STRING,
  47.   MC_GET_GLOBAL_STRING,
  48.   MC_SET_GLOBAL_STRING,
  49.   MC_GET_FIELD_BY_NAME_STRING,
  50.   MC_GET_FIELD_BY_NUMBER_STRING,
  51.   MC_GET_FIELD_BY_ID_STRING,
  52.   MC_SET_FIELD_BY_NAME_STRING,
  53.   MC_SET_FIELD_BY_NUMBER_STRING,
  54.   MC_SET_FIELD_BY_ID_STRING,
  55.   MC_SHOW_IMAGE_BY_NAME_STRING,
  56.   MC_SHOW_IMAGE_BY_NUMBER_STRING,
  57.   MC_SHOW_IMAGE_BY_ID_STRING,
  58.   MC_GET_VARIABLE_STRING,
  59.   MC_SET_VARIABLE_STRING,
  60.   MC_GET_VARIABLE_EX_STRING,
  61.   MC_SET_VARIABLE_EX_STRING
  62. ,
  63. MC_GET_ARRAY,
  64. MC_SET_ARRAY
  65. };
  66.  
  67. static Atom XCAtoms[MC_NUM_ATOMS];
  68. static Atom mcatom;
  69. static Atom idatom = None;
  70.  
  71. static char *XTretmess;
  72. static int XTretval;
  73.  
  74. #ifdef __STDC__
  75. static void propevent(Widget w, XtPointer client_data,
  76.               XEvent *event, Boolean *dispatch)
  77. #else
  78. static void propevent(w, client_data, event, dispatch)
  79. Widget w;
  80. XtPointer client_data;
  81. XEvent *event;
  82. Boolean *dispatch;
  83. #endif
  84. {
  85.   XPropertyEvent *pevent = (XPropertyEvent *)event;
  86.   if (pevent->state != PropertyNewValue)
  87.     return;
  88.   if (pevent->atom == (Atom)client_data) {
  89.     Atom type;
  90.     int format;
  91.     unsigned long nitems, extra;
  92.     unsigned char *uprop;
  93.     char *prop;
  94.     char *sptr;
  95.     XGetWindowProperty(MCdpy, MCwin, pevent->atom, 0,
  96.                XMAXPACK, False, XA_STRING, &type, &format,
  97.                &nitems, &extra, &uprop);
  98.     prop = (char *)uprop;
  99.     if (atol(prop) != (long)idatom) {
  100.       XFree(prop);
  101.       *dispatch = True;
  102.       return;
  103.     }
  104.     sptr = &prop[strlen(prop) + 1];
  105.     sptr += strlen(sptr) + 1;
  106.     XTretval = atoi(sptr);
  107.     sptr += strlen(sptr) + 1;
  108.     XTretmess = malloc(strlen(sptr) + 1);
  109.     strcpy(XTretmess, sptr);
  110.     XFree(prop);
  111.     *dispatch = False;
  112.   }
  113.   else
  114.     *dispatch = True;
  115. }
  116.  
  117. #ifdef __STDC__
  118. static char *event_wait(Atom reply, int *retval)
  119. #else
  120. static char *event_wait(reply, retval)
  121. Atom reply;
  122. int *retval;
  123. #endif
  124. {
  125.   XEvent event;
  126.   XtAppContext context;
  127.  
  128.   context = XtWidgetToApplicationContext(MCwidget);
  129.   XFlush(MCdpy);
  130.   XTretmess = NULL;
  131.   while (XTretmess == NULL) {
  132.     XtAppNextEvent(context, &event);
  133.     XtDispatchEvent(&event);
  134.   }
  135.   *retval = XTretval;
  136.   return XTretmess;
  137. }
  138.  
  139. #ifdef __STDC__
  140. static char *send_message(Atom atom, const char *arg1, const char *arg2,
  141.               const char *arg3, Bool pass, Bool error, int *reply)
  142. #else
  143. static char *send_message(atom, arg1, arg2, arg3, pass, error, reply)
  144.      Atom atom;
  145.      char *arg1;
  146.      char *arg2;
  147.      char *arg3;
  148.      Bool pass;
  149.      Bool error;
  150.      int *reply;
  151. #endif
  152. {
  153.   int i = 0;
  154.   int length = XATOMSIZE * 2 + strlen(arg1) + 7;
  155.   char *send;
  156.   if (arg2 != NULL)
  157.     length += strlen(arg2);
  158.   if (arg3 != NULL)
  159.     length += strlen(arg3);
  160.   send = malloc(length);
  161.   sprintf(&send[i], "%d", mcatom);
  162.   i += strlen(&send[i]) + 1;
  163.   sprintf(&send[i], "%d", idatom);
  164.   i += strlen(&send[i]) + 1;
  165.   sprintf(&send[i], "%d", (int)(pass != 0));
  166.   i += strlen(&send[i]) + 1;
  167.   sprintf(&send[i], "%d", (int)(error != 0));
  168.   i += strlen(&send[i]) + 1;
  169.   sprintf(&send[i], "%s", arg1);
  170.   if (arg2 != NULL) {
  171.     i += strlen(&send[i]) + 1;
  172.     sprintf(&send[i], "%s", arg2);
  173.   }
  174.   if (arg3 != NULL) {
  175.     i += strlen(&send[i]) + 1;
  176.     sprintf(&send[i], "%s", arg3);
  177.   }
  178.   i += strlen(&send[i]) + 1;
  179.   XtAddEventHandler(MCwidget, PropertyChangeMask, FALSE,
  180.             propevent, (XtPointer)atom);
  181.   XChangeProperty(MCdpy, MCwin, atom, XA_STRING, 8,
  182.           PropModeReplace, (unsigned char *)send, i);
  183.   free(send);
  184.   if (reply != NULL)
  185.     send = event_wait(atom, reply);
  186.   else
  187.     send = NULL;
  188.   XtRemoveEventHandler(MCwidget, PropertyChangeMask, FALSE,
  189.                propevent, (XtPointer)atom);
  190.   return send;
  191. }
  192.  
  193. #ifdef __STDC__
  194. void mc_xtinit(Widget ps, char *stack)
  195. #else
  196. void mc_xtinit(ps, stack)
  197. Widget ps;
  198. char *stack;
  199. #endif
  200. {
  201.   MCwidget = ps;
  202.   MCwin = XtWindow(ps);
  203.   MCdpy = XtDisplay(ps);
  204.   XSync(MCdpy, False);
  205.   fcntl(ConnectionNumber(MCdpy), F_SETFD, 1);
  206.   if ((MCpid = fork()) == 0) {
  207.     char wbuf[XINTSTRSIZE];
  208.     sprintf(wbuf, "%d", MCwin);
  209.     execlp("mc", "mc", "-w", wbuf, "-d", DisplayString(MCdpy), stack, NULL);
  210.     fprintf(stderr, "startup of mc with stack %s failed\n", stack);
  211.     exit(-1);
  212.   }
  213.   mcatom = XInternAtom(MCdpy, XCAtomNames[MC_ID], False);
  214.   idatom = XInternAtom(MCdpy, "XtApplication", False);
  215. }
  216.  
  217. void mc_xtclose()
  218. {
  219.   int status;
  220.   XFlush(MCdpy);
  221.   waitpid(MCpid, &status, 0);
  222. }
  223.  
  224. static char *callback(int id, const char *arg1, const char *arg2,
  225.               const char *arg3, int *retval)
  226. {
  227.   if (XCAtoms[id] == None)
  228.     XCAtoms[id] = XInternAtom(MCdpy, XCAtomNames[id], False);
  229.   return send_message(XCAtoms[id], arg1, arg2, arg3, False, False, retval);
  230. }
  231.  
  232. static char *getstatus()
  233. {
  234.   Atom type;
  235.   int format;
  236.   unsigned long nitems, extra;
  237.   unsigned char *uprop;
  238.   char *newprop;
  239.   Atom statatom = XInternAtom(MCdpy, MC_GET_MC_STATUS_STRING, False);
  240.   XGetWindowProperty(MCdpy, MCwin, statatom, 0,
  241.              XMAXPACK, False, XA_STRING, &type,
  242.              &format, &nitems, &extra, &uprop);
  243.   if (type == None)
  244.     return istrdup("startup");
  245.   newprop = (char *)uprop;
  246.   newprop = istrdup(newprop);
  247.   XFree(uprop);
  248.   return newprop;
  249. }
  250.  
  251. #include "XCmdFunc.c"
  252.